﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EPiServer.Personalization.VisitorGroups;

namespace CriteriaPack.RoleCriterion
{
    /// <summary>
    /// Implementation of a EPiServer.Personalization.VisitorGroups.CriterionBase
    /// which checks if a user is in a named role.
    /// </summary>
    [VisitorGroupCriterion(
        Category = "Technical Criteria",
        DisplayName = "Role",
        Description = "Criterion that matches the user's roles",
        LanguagePath = "/shell/cms/visitorgroups/criteria/role")]
    public class RoleCriterion : CriterionBase<RoleModel>
    {
        public override bool IsMatch(System.Security.Principal.IPrincipal principal,
                                     HttpContextBase httpContext)
        {
            bool isInRole = principal.IsInRole(Model.RoleName);
            bool shouldBeInRole = Model.Condition == RoleCompareCondition.Equal;
            return isInRole == shouldBeInRole;
        }

    }
}